{
 "metadata": {
  "signature": "sha256:0a01ca37cad2963dbba8a7af8426ac0a51c64c3391f311ae36092d38b2628bb0"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Finding the minimum point in the convex hull of a finite set of points\n",
      "======================================================================\n",
      "\n",
      "Based on the work of Philip Wolf [1] and the recursive algorithm of\n",
      "Kazuyuki Sekitani and Yoshitsugu Yamamoto [2].\n",
      "\n",
      "The algorithm in [2] has 3 epsilon to avoid comparison problems in three\n",
      "parts of the algorithm. The code below has few changes and only one\n",
      "epsilon. The aim of the change is to avoid infinite loops.\n",
      "\n",
      "Code\n",
      "----"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "from numpy import array, matrix, sin, sqrt, dot, cos, ix_, zeros, concatenate, abs, log10, exp, ones\n",
      "from numpy.linalg import norm\n",
      "\n",
      "from mpmath import mpf, mp\n",
      "mp.dps=80\n",
      "\n",
      "def find_min_point(P):\n",
      "#    print \"Calling find_min with P: \", P\n",
      "\n",
      "    if len(P) == 1:\n",
      "        return P[0]\n",
      "\n",
      "    eps = mpf(10)**-40\n",
      "\n",
      "    P = [array([mpf(i) for i in p]) for p in P]\n",
      "    \n",
      "    # Step 0. Choose a point from C(P)\n",
      "    x  = P[array([dot(p,p) for p in P]).argmin()]\n",
      "\n",
      "    while True:\n",
      "\n",
      "        # Step 1. \\alpha_k := min{x_{k-1}^T p | p \\in P}\n",
      "        p_alpha = P[array([dot(x,p) for p in P]).argmin()]\n",
      "\n",
      "        if dot(x,x-p_alpha) < eps:\n",
      "            return array([float(i) for i in x]) \n",
      "        \n",
      "        Pk = [p for p in P if abs(dot(x,p-p_alpha)) < eps]\n",
      "\n",
      "        # Step 2. P_k := { p | p \\in P and x_{k-1}^T p = \\alpha_k}\n",
      "        P_Pk = [p for p in P if not array([(p == q).all() for q in Pk]).any()]\n",
      "\n",
      "        if len(Pk) == len(P):\n",
      "            return array([float(i) for i in x]) \n",
      "\n",
      "        y = find_min_point(Pk)\n",
      "\n",
      "\n",
      "        p_beta = P_Pk[array([dot(y,p) for p in P_Pk]).argmin()]\n",
      "        \n",
      "        if dot(y,y-p_beta) < eps:\n",
      "            return array([float(i) for i in y]) \n",
      "\n",
      "        \n",
      "        # Step 4. \n",
      "        P_aux = [p for p in P_Pk if (dot(y-x,y-p)>eps) and (dot(x,y-p)!=0)]\n",
      "        p_lambda = P_aux[array([dot(y,y-p)/dot(x,y-p) for p in P_aux]).argmin()]\n",
      "        lam = dot(x,p_lambda-y) / dot(y-x,y-p_lambda)\n",
      "\n",
      "        x += lam * (y-x)\n",
      "\n",
      "\n",
      "\n",
      "if __name__ == '__main__':\n",
      "    print find_min_point( [array([ -4.83907292e+00,   2.22438863e+04,  -2.67496763e+04]), array([   9.71147604, -351.46404195, -292.18064276]), array([  4.60452808e+00,   1.07020174e+05,  -1.25310230e+05]), array([  2.16080134e+00,   5.12019937e+04,  -5.96167833e+04]), array([  2.65472146e+00,   6.70546443e+04,  -7.71619656e+04]), array([  1.55775358e+00,  -1.34347516e+05,   1.53209265e+05]), array([   13.22464295,  1869.01251292, -2137.61850989])])\n",
      "\n",
      "\n",
      "    print find_min_point( [array([ -4.83907292e+00,   2.22438863e+04,  -2.67496763e+04]), array([   9.71147604, -351.46404195, -292.18064276]), array([  4.60452808e+00,   1.07020174e+05,  -1.25310230e+05]), array([  2.16080134e+00,   5.12019937e+04,  -5.96167833e+04]), array([  2.65472146e+00,   6.70546443e+04,  -7.71619656e+04]), array([  1.55775358e+00,  -1.34347516e+05,   1.53209265e+05]), array([   13.22464295,  1869.01251292, -2137.61850989]), array([ 12273.18670123,  -1233.32015854,  61690.10864825])])"
     ],
     "language": "python",
     "metadata": {},
     "outputs": [
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "[ 13.0643029   -3.03446491  -2.65980139]\n",
        "[  1.61870596e-04  -3.78774039e-05  -3.29329552e-05]"
       ]
      },
      {
       "output_type": "stream",
       "stream": "stdout",
       "text": [
        "\n"
       ]
      }
     ],
     "prompt_number": 1
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "## References\n",
      "\n",
      "1. [Finding the nearest point in A polytope](http://www.springerlink.com/content/hw0l2n1271260604/)\n",
      "2. [A recursive algorithm for finding the minimum norm point in a polytope and a pair of closest points in two polytopes](http://www.springerlink.com/content/j25702174115q68x/)\n"
     ]
    }
   ],
   "metadata": {}
  }
 ]
}